import 'package:floor/floor.dart';
import 'package:flutter_2b_client/database/entity/GoodsScratch.dart';

import '../AppDatabase.dart';
import '../entity/GoodsScratchItem.dart';

@dao
abstract class GoodsScratchItemDao{

  @Query("select * from GoodsScratchItem where scratchId = :scratchId order by id")
  Future<List<GoodsScratchItem>> findPageByGoodsScratchId(int scratchId);

  @Insert(onConflict: OnConflictStrategy.replace)
  Future<int> insertGoodsScratchItem(GoodsScratchItem goodsScratchItem);

  @Query("delete GoodsScratchItem where scratchId = :scratchId")
  Future<void> deleteAllByGoodsScratch(int scratchId);

  @delete
  Future<void> deleteGoodsScratchItem(GoodsScratchItem goodsScratchItem);

  @Query("select count(*) from GoodsScratchItem where scratchId = :scratchId")
  Future<int?> countAllByGoodsScratchId(int scratchId);

  @transaction
  Future<void> deleteGoodsScratch(GoodsScratch scratch)async {
    var instance = await AppDatabase.instance;
    await instance.goodsScratchItemDao
        .deleteAllByGoodsScratch(scratch.id!);
    await instance.goodsScratchDao.deleteGoodsScratch(scratch);
  }

}

